package Utils;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/7/26 11:25
 * @since JDK 1.8
 */
public class Base64Utils {



    public static void main(String[] args) {
        String username = encode("admin");
        System.out.println(username);
        String username1=decode(username);
        System.out.println(username1);
    }

    private final static Base64.Encoder encoder = Base64.getEncoder();

    private final static Base64.Decoder decoder = Base64.getDecoder();

    /**
     * 给字符串加密
     * @param text
     * @return
     */
    public static String encode(String text) {
        byte[] textByte = new byte[0];
        try {
            textByte = text.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String encodedText = encoder.encodeToString(textByte);
        return encodedText;
    }

    /**
     * 将加密后的字符串进行解密
     * @param encodedText
     * @return
     */
    public static String decode(String encodedText) {
        String text = null;
        try {
            text = new String(decoder.decode(encodedText), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return text;
    }
}
